PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Try

A Try statement is a compound statement consisting of a list of AppleScript statements followed by an error handler to be executed if any of the statements cause an error message.

SYNTAX
try
    [ statement ]...
on error                                            ¬
        [ errorMessageVariable ]                    ¬
        [ number errorNumberVariable ]              ¬
        [ from offendingObjectVariable ]            ¬
        [ partial result resultListVariable ]       ¬
        [ to expectedTypeVariable ]
    [ global variable [, variable ]...]
    [ local variable [, variable ]...]
    [ statement ]...
end [ error | try ]

where

statement is any AppleScript statement.

errorMessageVariable (an identifier) You use this parameter variable within the error handler to refer to the error expression, usually a string, that describes the error.

errorNumberVariable (an identifier) You use this parameter variable within the error handler to refer to the error number, an integer.

offendingObjectVariable (an identifier) You use this parameter variable within the error handler as a reference to the application object that caused the error.

resultListVariable (an identifier) You use this parameter variable within the error handler to refer to partial results for objects that were handled before the error occurred. Its value is a list that can contain values of any class. This parameter applies only to commands that return results for multiple objects. For example, if an application handles the command get words 1 thru 5 and an error occurs when handling word 4, the partial result parameter contains the results for the first three words.

expectedTypeVariable (an identifier) This parameter variable identifies the expected value class (a class identifier)--that is, the value class to which AppleScript was attempting to coerce the value of offendingObjectVariable. If an application receives data of the wrong class and cannot coerce it to the correct class, the value of this parameter variable is the class of the coercion that failed. (The example at the end of this definition demonstrates how this works.)

variable (an identifier) This parameter variable is either a global variable or a local variable that can be used in the handler. The scope of a local variable is the handler. You cannot refer to a local variable outside the handler. The scope of a global variable can extend to any other part of the script, including other handlers and script objects. For detailed information about the scope of local and global variables, see Scope of Script Variables and Properties.

EXAMPLES

The following Try statement provides an error handler for the Choose File command, one of the scripting addition commands distributed with AppleScript. The Choose File command returns an error if the user clicks the Cancel button in the Choose File dialog box. If the user doesn't choose a file, the error handler asks whether to continue, using a default file. If the user chooses to continue, a second dialog confirms the choice and displays the name of the default file.

set fileName to "Generic Prefs" -- Use if no filename chosen.
try
    choose file -- Ask user to choose a file.
    -- If the user cancels, the next statement won't be executed.
    set fileName to result
on error errText number errNum
    if (errNum is equal to -128) then -- User cancelled.
        display dialog "No file chosen. Use the default file?"
        if button returned of result is equal to "OK" then
            display dialog "The script will continue " & ¬
                "using the default file \"" & fileName & "\"."
        end if
    else
        -- If any other error, do nothing.
    end if
end try

The next example demonstrates the use of the To keyword to capture additional information about an error that occurs during a coercion failure.

try
    repeat with i from 1 to "Toronto"
        i
    end repeat
on error from obj to newClass
    log {obj, newClass} -- Display from and to info in log window.
end try

The Repeat statement fails because the string "Toronto" is the wrong class--it's a string, not an integer. The error handler simply writes the values of obj (the offending value, "Toronto" ) and newClass (the class of the coercion that failed, integer ) to the Script Editor's Event Log window. The result is "(*Toronto, integer*)", indicating the error occurred while trying to coerce "Toronto" to an integer. You can open the Script Editor's Event Log window from the Controls menu or by typing Command-E. For more information on using the Event Log window, see Debugging Scripts and the AppleScript section of the Mac OS Help Center.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)